/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tech15.task.dao;

import java.util.*;

import javacommon.base.*;
import javacommon.util.*;

import cn.org.rapid_framework.util.*;
import cn.org.rapid_framework.web.util.*;
import cn.org.rapid_framework.page.*;
import cn.org.rapid_framework.page.impl.*;

import com.tech15.core.model.SysUser;
import com.tech15.task.model.*;
import com.tech15.task.dao.*;
import com.tech15.task.service.*;
import com.tech15.task.vo.query.*;

/**
 * @author 胡作正 email:zuozhenghu@sina.com
 * @version 1.0
 * @since 1.0
 */


import static cn.org.rapid_framework.util.ObjectUtils.*;
import org.springframework.stereotype.Repository;

@Repository
public class TaskDeptDao extends BaseHibernateDao<TaskDept,Long>{

	public Class getEntityClass() {
		return TaskDept.class;
	}
	
	public Page findByPageRequest(PageRequest<Map> pageRequest) {
		//XsqlBuilder syntax,please see http://code.google.com/p/rapid-xsqlbuilder
		// [column]为字符串拼接, {column}为使用占位符. 以下为图方便采用sql拼接,适用性能要求不高的应用,使用占位符方式可以优化性能. 
		// [column] 为PageRequest.getFilters()中的key
		String sql = "select t from TaskDept t where 1=1 "
				+ "/~ and t.taskid = '[taskid]' ~/"
				+ "/~ and t.deptid = '[deptid]' ~/"
				+ "/~ order by [sortColumns] ~/";
		return pageQuery(sql,pageRequest);
	}
	
	public Page findPage(TaskDeptQuery query) {
        //XsqlBuilder syntax,please see http://code.google.com/p/rapid-xsqlbuilder
        // [column]为字符串拼接, {column}为使用占位符. [column]为使用字符串拼接,如username='[username]',偷懒时可以使用字符串拼接 
        // [column] 为PageRequest的属性
		String sql = "select t from TaskDept t where 1=1 "
			  	+ "/~ and t.taskid = {taskid} ~/"
			  	+ "/~ and t.deptid = {deptid} ~/"
				+ "/~ order by [sortColumns] ~/";
				
        //生成sql2的原因是为了不喜欢使用xsqlbuilder的同学，请修改生成器模板，删除本段的生成
        StringBuilder sql2 = new StringBuilder("select t from TaskDept t where 1=1 ");
        if(isNotEmpty(query.getTdid())) {
            sql2.append(" and  t.tdid = :tdid ");
        }
        if(isNotEmpty(query.getTaskid())) {
            sql2.append(" and  t.taskid = :taskid ");
        }
        if(isNotEmpty(query.getDeptid())) {
            sql2.append(" and  t.deptid = :deptid ");
        }
        if(isNotEmpty(query.getSortColumns())) {
            sql2.append(" order by :sortColumns ");
        }	
        
		return pageQuery(sql,query);
	}
	
	public List<SysUser> searchTaskUserDept(Long taskid,Integer sendtype){
		String sql = "select t.* from sys_user t,task_dept t1 where t1.sendtype="+sendtype+" and t.taskdeptid=t1.deptid and t.userrole=0 and t1.taskid="+taskid;
		Map map = new HashMap();
		map.put("userid","Long");
		map.put("realname", "String");
		return findSqlAll(sql, map, SysUser.class);
	}
	
	
	public List<TaskDept> searchTaskDept(Long taskid,Integer sendtype){
		String sql = "select t.*,t1.deptname as orgInfo from task_dept t left join sys_dept t1 on t.deptid=t1.deptid where t.taskid="+taskid+" and t.sendtype="+sendtype;
		Map map = new HashMap();
		map.put("deptid","Long");
		map.put("orgInfo", "String");
		return findSqlAll(sql, map, TaskDept.class);
	}
	
}
